﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace CourseSystem.Stat
{
    public class UrlMapping
    {
        public static Dictionary<string, string> StudentStatUrl;
        public static Dictionary<string, string> TeacherStatUrl;

        static UrlMapping()
        {
            Init();
        }

        private static void Init()
        {
            //StudentStatUrl.Add("", "");
            StudentStatUrl = new Dictionary<string, string>(50);
            StudentStatUrl.Add("home/index", "首页");
            StudentStatUrl.Add("guide/news", "课程导学-课程通知");
            StudentStatUrl.Add("guide/index/1", "课程导学-课程介绍");
            StudentStatUrl.Add("guide/teacher", "课程导学-教师简介");
            StudentStatUrl.Add("guide/index/3", "课程导学-教学大纲");
            StudentStatUrl.Add("guide/index/4", "课程导学-教学安排");
            StudentStatUrl.Add("guide/index/5", "课程导学-学习方法");

            StudentStatUrl.Add("study/index", "课程学习-课程章节");
            StudentStatUrl.Add("study/case", "课程学习-案例");
            StudentStatUrl.Add("study/ppt", "课程学习-教师PPT");
            StudentStatUrl.Add("study/courseware", "课程学习-讲授式课件");
            StudentStatUrl.Add("study/prospect", "课程学习-学科进展");
            StudentStatUrl.Add("study/internet", "课程学习-Internet资源");

            StudentStatUrl.Add("examlib/homework?type=1", "作业测验-作业");
            StudentStatUrl.Add("examlib/homework?type=0", "作业测验-练习");
            StudentStatUrl.Add("examlib/homework?type=2", "作业测验-整门课程练习");
            StudentStatUrl.Add("examlib/homework?type=3", "作业测验-整门课程作业");
            StudentStatUrl.Add("examlib/exam", "作业测验-考试");
            StudentStatUrl.Add("examlib/excellent", "作业测验-优秀作业展");
            StudentStatUrl.Add("examlib/score?type=0", "作业测验-练习成绩查询");
            StudentStatUrl.Add("examlib/score?type=1", "作业测验-作业成绩查询");
            StudentStatUrl.Add("examlib/score?type=4", "作业测验-考试成绩查询");

            StudentStatUrl.Add("resources/index?type=1", "课程资源库-文本");
            StudentStatUrl.Add("resources/index?type=2", "课程资源库-图像");
            StudentStatUrl.Add("resources/index?type=3", "课程资源库-Flash");
            StudentStatUrl.Add("resources/index?type=4", "课程资源库-音频");
            StudentStatUrl.Add("resources/index?type=5", "课程资源库-视频");
            StudentStatUrl.Add("resources/index?type=6", "课程资源库-PPT");
            StudentStatUrl.Add("resources/index?type=7", "课程资源库-学习包");

            StudentStatUrl.Add("interaction/question?iscommon=0", "师生交互-师生问答");
            StudentStatUrl.Add("interaction/question?iscommon=1", "师生交互-常见问题库");
            StudentStatUrl.Add("interaction/forums?isdigest=0", "师生交互-主题讨论");
            StudentStatUrl.Add("interaction/forums?isdigest=1", "师生交互-精华帖展示");

            StudentStatUrl.Add("support/index?type=0", "学习支持-导航");
            StudentStatUrl.Add("support/index?type=1", "学习支持-课程使用帮助");
            StudentStatUrl.Add("support/index?type=2", "学习支持-学习进程");
            StudentStatUrl.Add("support/index?type=3", "学习支持-笔记本");
            StudentStatUrl.Add("support/index?type=4", "学习支持-常用软件");

            StudentStatUrl.Add("comment/index", "课程评价-评价");

            //教师
            TeacherStatUrl = new Dictionary<string, string>(50);
            TeacherStatUrl.Add("setting/index", "课程管理");

            TeacherStatUrl.Add("adminguide/notice", "课程导学-课程通知");
            TeacherStatUrl.Add("adminguide/index", "课程导学-设置");
            TeacherStatUrl.Add("adminguide/menusub/1", "课程导学-课程介绍");
            TeacherStatUrl.Add("adminguide/userlist", "课程导学-教师简介");
            TeacherStatUrl.Add("adminguide/menusub/3", "课程导学-教学大纲");
            TeacherStatUrl.Add("adminguide/menusub/4", "课程导学-教学安排");
            TeacherStatUrl.Add("adminguide/menusub/5", "课程导学-学习方法");

            TeacherStatUrl.Add("adminstudy/chapter", "课程学习-章节学习设置");
            TeacherStatUrl.Add("adminstudy/case", "课程学习-案例");
            TeacherStatUrl.Add("adminstudy/ppt", "课程学习-教师PPT");
            TeacherStatUrl.Add("adminstudy/courseware", "课程学习-讲授式课件");
            TeacherStatUrl.Add("adminstudy/prospect", "课程学习-学科进展");
            TeacherStatUrl.Add("adminstudy/internet", "课程学习-Internet资源");

            TeacherStatUrl.Add("adminexamlib/lib", "作业测验-题库管理");
            TeacherStatUrl.Add("adminexamlib/sectiontest", "作业测验-习题与作业管理");
            TeacherStatUrl.Add("adminexamlib/examination?type=4", "作业测验-考试试卷");
            TeacherStatUrl.Add("adminexamlib/examiner", "作业测验-考试监考");
            TeacherStatUrl.Add("adminexamlib/scoring", "作业测验-阅卷打分");
            TeacherStatUrl.Add("adminexamlib/examresult", "作业测验-成绩导出");
            TeacherStatUrl.Add("adminexamlib/student", "作业测验-评价学生");
            
            TeacherStatUrl.Add("adminresources/common?type=1", "课程资源库-文本");
            TeacherStatUrl.Add("adminresources/common?type=2", "课程资源库-图像");
            TeacherStatUrl.Add("adminresources/common?type=3", "课程资源库-Flash");
            TeacherStatUrl.Add("adminresources/common?type=4", "课程资源库-音频");
            TeacherStatUrl.Add("adminresources/common?type=5", "课程资源库-视频");
            TeacherStatUrl.Add("adminresources/common?type=6", "课程资源库-PPT");
            TeacherStatUrl.Add("adminresources/common?type=7", "课程资源库-学习包");

            TeacherStatUrl.Add("admininteraction/question", "师生交互-师生问答");
            TeacherStatUrl.Add("admininteraction/forums", "师生交互-主题讨论");

            TeacherStatUrl.Add("adminsupport/guide", "学习支持-导航");
            TeacherStatUrl.Add("adminsupport/helper", "学习支持-课程使用帮助");
            TeacherStatUrl.Add("adminsupport/process", "学习支持-学习进度");
            TeacherStatUrl.Add("adminsupport/software", "学习支持-常用软件");

            //TeacherStatUrl.Add("admincomment/index", "评价管理-教师评价");
            //TeacherStatUrl.Add("admincomment/teacherstat", "评价管理-教师评价统计");
            TeacherStatUrl.Add("admincomment/coursecomment", "评价管理-课程评价");
            TeacherStatUrl.Add("admincomment/coursestat", "评价管理-课程评价统计");

            TeacherStatUrl.Add("admin/college", "学生管理-学院");
            TeacherStatUrl.Add("admin/student", "学生管理-学生");

            TeacherStatUrl.Add("adminmonitor/student", "用户监控-学生监控");
            TeacherStatUrl.Add("adminmonitor/studenthours", "用户监控-学生学时监控");
        }

        private static readonly Regex StudyReg = new Regex(@"study/index/\d*", RegexOptions.Compiled);

        public static void InitUrl(ref string url)
        {
            //空地址和有地址的排除
            if (string.IsNullOrEmpty(url) || StudentStatUrl.ContainsKey(url))
                return;
            var point = url.IndexOf('?');
            if (url.StartsWith("guide/index/"))
            {
                if (point < 0)
                    return;
                url = url.Substring(0, point);
                return;
            }
            if (url == "study/index")
            {
                url = url + "?type=0";
                return;
            }
            if (StudyReg.IsMatch(url))
            {
                url = StudyReg.Replace(url, "study/index");
                if (url.Contains("type=0"))
                    url = url.Replace("&iss=1", "");
                return;
            }
            if (url.StartsWith("examlib/examdetail/"))
            {
                if (HttpContext.Current.Request.UrlReferrer != null && HttpContext.Current.Request.UrlReferrer.LocalPath.IndexOf("examlib/exam", StringComparison.CurrentCultureIgnoreCase) > -1)
                    url = "examlib/exam";
                else
                    url = "examlib/exercise";
                return;
            }
            if (url.StartsWith("resources/index?type="))
            {
                var andIndex = url.IndexOf('&');
                if (andIndex > -1)
                    url = url.Substring(0, andIndex);
                return;
            }
        }
    }
}
